#define _CRT_SECURE_NO_WARNINGS   1

#include <iostream>
#include<string>
#include<algorithm>
using namespace std;
const int N = 1e3 + 5;
int dp[N][N] = { 0 };
int n, m;
string a, b;
int main()
{
	cin >> n >> m;
	cin >> a >> b;
	for (int i = 0; i<n; i++){
		for (int j = 0; j<m; j++){
			if (a[i] == b[j]){
				dp[i + 1][j + 1] = dp[i][j] + 1;
			}
			else{
				dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
			}
		}
	}
	cout << dp[n][m] << endl;
	return 0;
}
